package gis.lifay.sdk.geoserver;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSON;
import it.geosolutions.geoserver.rest.GeoServerRESTManager;
import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;
import it.geosolutions.geoserver.rest.GeoServerRESTReader;
import it.geosolutions.geoserver.rest.encoder.GSResourceEncoder;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 *@ClassName GeoSdk
 *@Description TODO
 *@Author lifay
 *@Date 2021/11/2 20:34
 **/
public class GeoSdk {

    private GeoServerRESTManager manager;
    private GeoServerRESTPublisher publisher;
    private GeoServerRESTReader restReader;
    private String url = "http://localhost:8070/geoserver";
    private String user = "admin";
    private String pw = "geoserver";

    public GeoSdk(String url) throws MalformedURLException {
        this.url = url;
        manager = new GeoServerRESTManager(new URL(this.url), this.user, this.pw);
        publisher = manager.getPublisher();
        restReader = manager.getReader();
    }

    public GeoSdk(String url, String user, String pw) throws MalformedURLException {
        this.url = url;
        this.user = user;
        this.pw = pw;
        manager = new GeoServerRESTManager(new URL(this.url), this.user, this.pw);
        publisher = manager.getPublisher();
        restReader = manager.getReader();
    }

    public String publishGeoTIFF(String workspace, String dataStore, String converage, String filePath) throws FileNotFoundException {
        Assert.notNull(workspace);
        Assert.notNull(dataStore);
        Assert.notNull(converage);
        if (!restReader.existsWorkspace(workspace)) {
            publisher.createWorkspace(workspace);
        }
        File file = new File(filePath);
        if (!FileUtil.exist(file)) {
            throw new FileNotFoundException(file.getAbsolutePath());
        }
        if (restReader.existsCoveragestore(workspace, converage)) {
            //throw new RuntimeException("发布失败,服务已存在： " + workspace + " + " + converage);
            publisher.removeCoverageStore(workspace, converage);
        }
        double[] bbox = {-180, -90, 180, 90};
        boolean isPublise = false;
        try {
            isPublise = publisher.publishGeoTIFF(workspace, dataStore, converage, file,
                    "EPSG:4326", GSResourceEncoder.ProjectionPolicy.REPROJECT_TO_DECLARED, "raster", bbox);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("发布失败：" + e.getMessage());
        }
        if (!isPublise) {
            throw new RuntimeException("发布失败");
        }
        Map<String, Object> datas = new LinkedHashMap<>();
        datas.put("url", "/" + workspace + "/wms");
        datas.put("layers", workspace + ":" + converage);
        datas.put("format", "image/jpeg");
        datas.put("srs", "EPSG:4326");
        datas.put("styles", "");
        return JSON.toJSONString(datas);
    }

    /**
     * 删除指定服务
     * @param workspace
     * @param dataStore
     * @param converage
     * @return
     * @throws FileNotFoundException
     */
    public boolean removeCoverageStore(String workspace, String dataStore, String converage) {
        Assert.notNull(workspace);
        Assert.notNull(dataStore);
        Assert.notNull(converage);
        StringBuilder sb = new StringBuilder();
        if (restReader.existsWorkspace(workspace)) {
            if (!existsCoveragestore(workspace, converage)) {
                return true;
            }
            return publisher.removeCoverageStore(workspace, converage, true);
        }
        return false;
    }

    public boolean existsCoveragestore(String workspace, String converage) {
        return restReader.existsCoveragestore(workspace, converage);
    }

    public void query(String workspace, String dataStore, String converage) {
        boolean a = restReader.existsDatastore(workspace, dataStore);
        boolean b = restReader.existsCoveragestore(workspace, converage);
        boolean c = restReader.existsCoverage(workspace, dataStore, converage);
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }

    public void close() {
        manager = null;
        restReader = null;
        publisher = null;
    }
}